package com.hopu.blb_erp.controller;


import com.hopu.blb_erp.entity.Menu;
import com.hopu.blb_erp.service.IMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xxl
 * @since 2021-11-22
 */
@Api
@RestController
public class MenuController {
    @Autowired
    private IMenuService menuService;
    @GetMapping("/menus/tree")
    public ResponseEntity<List<Menu>> findTree(){
        List<Menu> menuTree = menuService.getMenuTree();
        return ResponseEntity.ok(menuTree);
    }
    @PostMapping("/menu/pid/{pid}")
    public ResponseEntity<String> addMenuByPId(@RequestBody Menu menuaa,@PathVariable("pid") Integer pid){
        menuaa.setPid(pid);
        menuService.save(menuaa);
        return ResponseEntity.ok("ok");
    }
    //添加仓库
    @PostMapping("/menu/id/{id}")
    public ResponseEntity<String> addMenuById(@RequestBody Menu menuaa,@PathVariable("id") Integer id){
        menuaa.setPid(id);
        menuService.save(menuaa);
        return ResponseEntity.ok("ok");
    }
    //更新仓库
    @PutMapping("/menuu")
    public ResponseEntity<String> modifyMenu(@RequestBody Menu menu){
       menuService.saveOrUpdate(menu);
        return ResponseEntity.ok("ok");
    }
    @ApiOperation("删除上下级的类")
    @DeleteMapping("/menuu/{id}")
    public ResponseEntity<String> removeMenu(@PathVariable("id") Integer id){
        menuService.removeByPidAddId(id);
        return ResponseEntity.ok("ok");
    }
}
